#!/bin/sh
source /home/mysql/.bash_profile
DBHOST=`hostname`
port=""
[ ! -e  /u01/mysql/bin/mysql ]  && echo "no mysql"  && exit 0
[ ! -e /opt/MegaRAID/MegaCli/MegaCli64 ] && yum install -y MegaCli
MYSQL="/u01/mysql/bin/mysql -umonitor -ptb4monitor -h172.23.110.33 -P3306 tianji -e "
#instance_info
#instance_name   ,ip    ,data_space   ,hard_type  ,mem  ,core_num
get_base() {
  if [ $1 -eq 3306 ]; then
  basedir="/u01/mysql"
  elif [ $1 -eq 3307 ]; then
  basedir="/u01/mysql2"
  elif [ $1 -eq 3406 ]; then
  basedir="/u01/mysql2"
  elif [ $1 -eq 3506 ]; then
  basedir="/u01/mysql3"
  elif [ $1 -eq 3606 ]; then
  basedir="/u01/mysql4"
  else
  echo "no port set"
  fi
}

ports=`ps -ef |grep mysqld|grep port=|awk -F"port=" '{print $NF}'|grep -v awk|awk '{print $1}'|grep -v awk`
insnum=`echo "$ports"|wc -l`
[ $insnum -eq 0 ] && echo "no mysql instance" && exit 0
inslog=$2
[ -z $inslog ]  &&  inslog="/tmp/tmp_ins`date +%Y%m%d`"
[ $insnum -eq 0  ] && echo "no mysql " && exit 9;
  nownum=0
  for port in $ports
     do
     tmpnum=`/u01/mysql/bin/mysql -h127.0.0.1 -uroot -P$port -e  "show variables like 'read_only';"|grep ON|wc -l`
     [ $tmpnum -eq 1 ] &&  let nownum+=1
  done
[ $nownum -lt $insnum ] && echo "ERROR"   && echo "some are not read_only!!"  && exit 99

ip=`hostname -i`
FIOv=`/sbin/lspci  |grep -i Fusion-io|wc -l`
SSDv=`/opt/MegaRAID/MegaCli/MegaCli64  -PDList  -aAll -NoLog|grep -i ssd|wc -l`
if [  $SSDv -gt 0  ];then 
	hard_type=ssd
elif [  $FIOv  -gt 0 ];then
	hard_type=fio
else
	hard_type='normal'
fi
for port in $ports
   do
   	inst_name="$DBHOST:$port"
   	get_base $port
   	space=`du -s $basedir/data|awk '{print $1}'`
    bpool=`cat $basedir/my.cnf |grep buffer_pool|grep -v '#'|tail -n 1 |awk -F= '{print $2}'`
   	mem=`free -g|grep -i Mem|awk '{printf $2"G"}'`
    core_num=`cat /proc/cpuinfo |grep processor|wc -l`
    sql="replace  into instance_info  (instance_name,ip,data_space,hard_type,mem,core_num,port,bpoll_size) values ('$inst_name','$ip','$space','$hard_type','$mem','$core_num','$port','$bpool');"
    $MYSQL "$sql"
done